import Vue from 'vue'
import VueRouter from 'vue-router'
const Index = () => import('@/views/Index')
const Home = () => import('@/views/Home')
const Login = () => import('@/views/Login')
const Order = () => import('@/views/Order')
const Me = () => import('@/views/Me')
const Address = () => import('@/views/Address')
const City = () => import('@/views/City')
const Search = () => import('@/views/Search')
const DetailShop = () => import('@/views/DetailShop/ShopDetail')
const OrderMenu = () => import('@/views/DetailShop/OrderMenu')
const Merchant = () => import('@/views/DetailShop/Merchant')
const Comment = () => import('@/views/DetailShop/Comment')
const MyAddress = () => import('@/views/Address/myAddress')
const AddAddress = () => import('@/views/Address/addAddress')
const Settlement = () => import('@/views/Address/Settlement')
const Remark = () => import('@/views/Address/Remark')
const Pay = () => import('@/views/Address/Pay')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Index,
    children: [
      {
        path: '',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'home',
        component: Home
      },
      {
        path: '/order',
        name: 'order',
        component: Order
      },
      {
        path: '/me',
        name: 'me',
        component: Me
      },
      {
        path: '/address',
        name: 'address',
        component: Address
      },
      {
        path: '/city',
        name: 'city',
        component: City
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/search',
    name: 'search',
    component: Search
  },
  {
    path: '/detail',
    name: 'detail',
    component: DetailShop,
    redirect: '/orderMenu',
    children: [
      {
        path: '/orderMenu',
        component: OrderMenu
      },
      {
        path: '/merchant',
        component: Merchant
      },
      {
        path: '/comment',
        component: Comment
      }
    ]
  },
  {
    path: '/myAddress',
    name: 'myAddress',
    component: MyAddress
  },
  {
    path: '/addAddress',
    name: 'addAddress',
    component: AddAddress,
    meta: {
      keepAlive: false
    }
  },
  {
    path: '/settlement',
    name: 'settlement',
    component: Settlement
  },
  {
    path: '/remark',
    name: 'remark',
    component: Remark
  },
  {
    path: '/pay',
    name: 'pay',
    component: Pay
  }
]
const router = new VueRouter({
  mode: 'history',
  linkActiveClass: 'active',
  base: process.env.BASE_URL,
  routes
})

// 路由导航全局前置守卫
router.beforeEach((to, from, next) => {
  const isLogin = sessionStorage.ele_login
  if (to.path === '/login') {
    next()
  }
  else {
    // 是否在登录状态下
    isLogin ? next() : next('/login')
  }
})

export default router
